package com.woniuxy.itrip.hotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.commonutils.R;
import com.woniuxy.itrip.hotel.entity.ItripHotel;
import com.woniuxy.itrip.hotel.entity.vo.HotelQuery;
import com.woniuxy.itrip.hotel.service.ItripHotelService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 酒店表 前端控制器
 * </p>
 *
 * @author Giles
 * @since 2020-07-26
 */
@RestController
@RequestMapping("/hotel/itrip-hotel")
public class ItripHotelController {
    @Autowired
    private ItripHotelService hotelService;
    @ApiOperation(value = "查询所有酒店信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = R.class,notes = "查询所有酒店信息")
    @GetMapping(value = "/selectAllHotel")
    public R selectAllHotel(){
        List<ItripHotel> all=this.hotelService.list();
        if(all!=null&&all.size()>0){
            return R.ok().data("items",all);
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "分页查询酒店信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = R.class,notes = "分页查询酒店信息")
    @GetMapping(value = "/selectHotelPage/{current}/{limit}")
    public R selectHotelPage(@ApiParam(required = true,name = "current",value = "当前页") @PathVariable long current,
                             @ApiParam(required = true,name = "limit",value = "每页的记录数")  @PathVariable long limit){
        Page<ItripHotel> page=new Page<ItripHotel>(current,limit);
        hotelService.page(page,null);
        long total = page.getTotal();
        List<ItripHotel> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    @CrossOrigin
    @ApiOperation(value = "分页条件查询酒店信息",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = R.class,notes = "分页条件查询酒店信息")
    @GetMapping(value = "/selectHotelPageWithCodition/{current}/{limit}")
    public R selectHotelPageWithCodition(@ApiParam(required = true,name = "current",value = "当前页") @PathVariable long current,
                                         @ApiParam(required = true,name = "limit",value = "每页的记录数") @PathVariable long limit,
                                         HotelQuery hotelQuery){
        Page<ItripHotel> page=new Page<ItripHotel>(current,limit);
        QueryWrapper<ItripHotel> wrapper=new QueryWrapper<>();
        String hotelname=hotelQuery.getHotelName();
        Integer hoteLevel=hotelQuery.getHotelLevel();
        Integer cityId=hotelQuery.getCityId();
        if(!StringUtils.isEmpty(hotelname)){
            wrapper.like("hotelName",hotelname);
        }
       if(hoteLevel!=null){
            wrapper.eq("hotelLevel",hoteLevel);
        }
        if(cityId!=null){
            wrapper.eq("cityId",cityId);
        }
        hotelService.page(page,wrapper);
        long total = page.getTotal();
        List<ItripHotel> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }
    @CrossOrigin
    @ApiOperation(value = "按编号删除",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = R.class,notes = "按编号删除")
    @DeleteMapping(value = "/deleteHotel/{id}")
    public R deleteHotel(@PathVariable Integer id){
        boolean flag=hotelService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @CrossOrigin
    @ApiOperation(value = "按ID查找酒店",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = R.class,notes = "按ID查找酒店")
    @GetMapping(value = "/selectHotelById/{id}")
    public R selectHotelById(@PathVariable Integer id){
        ItripHotel hotel=hotelService.getById(id);
        if(hotel!=null){
            return R.ok().data("row",hotel);
        }else{
            return R.error();
        }
    }
}

